//
//  STPageControl.swift
//  StarTimesON
//
//  Created by Toj on 6/24/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class STPageControl: UIControl {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var numberOfPages: Int = 0 {
        didSet {
            clearIndicators()
            if numberOfPages == 0 { return }
            setupIndicators()
            
            isHidden = numberOfPages <= 1 && hidesForSinglePage
        }
    }
    
    public var indicatorWidth: CGFloat = 6
    public var indicatorMargin: CGFloat = 6

    public var currentPage: Int = 0 {
        didSet {
            // print("-->toj: currentPage: \(currentPage)")
            let index = currentPage < 0 ? 0 : currentPage
            if index >= indicators.count {
                return
            }
            currentIndicator = indicators[index]
        }
    }
    public var hidesForSinglePage: Bool = true
    
    public var pageIndicatorImage: UIImage?
    public var pageIndicatorColor: UIColor = .red
    
    public var currentPageIndicatorImage: UIImage?
    public var currentPageIndicatorColor: UIColor? = .blue
    

    public var sizeForNumberOfPages: CGSize {
        get {
            let width = indicatorWidth * CGFloat(numberOfPages) + (CGFloat(numberOfPages) - 1) * indicatorMargin
            let height = indicatorWidth
            return CGSize(width: width, height: height)
        }
    }
    
    private var currentIndicator: UIImageView? {
        willSet {
            currentIndicator?.image = pageIndicatorImage
            currentIndicator?.backgroundColor = pageIndicatorColor
            
            newValue?.image = currentPageIndicatorImage
            newValue?.backgroundColor = currentPageIndicatorColor
        }
    }
    private var indicators: [UIImageView] = []
    private func setupIndicators() {
        for i in 0..<numberOfPages {
            let v = UIImageView()
            v.layer.cornerRadius = indicatorWidth * 0.5
            v.image = pageIndicatorImage
            v.backgroundColor = pageIndicatorColor
            if i == currentPage {
                v.backgroundColor = currentPageIndicatorColor
                v.image = currentPageIndicatorImage
            }
            indicators.append(v)
            addSubview(v)
        }
    }
    
    private func clearIndicators() {
        indicators.forEach { view in
            view.removeFromSuperview()
        }
        indicators = []
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let swidth = frame.width
        let sheight = frame.height
        let leftMargin = swidth - sizeForNumberOfPages.width
        let y = (sheight - sizeForNumberOfPages.height) * 0.5
        var index = 0
        var x = leftMargin
        indicators.forEach { view in
            x = leftMargin + (CGFloat(index) * (indicatorWidth + indicatorMargin))
            index += 1
            view.frame = CGRect(x: x, y: y, width: indicatorWidth, height: indicatorWidth)
        }
    }
}
